package com.example.securitycheck;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.securitycheck.common.YhxxEntity;
import com.example.securitycheck.util.HttpUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button login;
    private TextView registrationBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //禁止旋转屏幕
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        init();

        //登录点击事件
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String u = username.getText().toString().trim();
                String p = password.getText().toString().trim();
                if (u.equals("")||p.equals("")){
                    Toast.makeText(MainActivity.this,"密码或账号不能为空", Toast.LENGTH_SHORT).show();//弹窗提醒功能
                }else {
                    final Map<String ,String> map = new HashMap<>();
                    map.put("name",u);
                    map.put("password",p);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                          final JSONObject jsonObject = HttpUrl.requestGet(map,"/yhxx/login");
                            System.out.println(jsonObject);
                            try {
                                int code = jsonObject.getInt("code");
                                final String msg = jsonObject.getString("msg");
                                if (code == 0){
                                    JSONObject user = jsonObject.getJSONObject("page");

                                    if (null==user){
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(MainActivity.this,"用户名或密码错误!", Toast.LENGTH_SHORT).show();//弹窗提醒功能
                                            }
                                        });
                                    }else{
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(MainActivity.this,"成功!", Toast.LENGTH_SHORT).show();//弹窗提醒功能
                                            }
                                        });
                                        //用户权限判断  职务（0:管理员，1:排查员，2:整改员）
                                        int job = user.getInt("job");
                                        if (job == 0){
                                            Intent intent = new Intent(MainActivity.this,ManagerActivity.class);
                                            startActivity(intent);
                                            finish();
                                        }else if(job == 1){
                                            Intent intent = new Intent(MainActivity.this,PaichaActivity.class);
                                            startActivity(intent);
                                            finish();
                                        }else {
                                            Intent intent = new Intent(MainActivity.this,ChangeActivity.class);
                                            startActivity(intent);
                                            finish();
                                        }

                                    }

                                }else{
                                    //弹框显示需要从子线程回到主线程显示，不然会报错
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show();//弹窗提醒功能
                                        }
                                    });
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        });
        //注册点击事件
        registrationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
    }
    //单例模式，初始化登录页的所有界面组件
    private void init(){
        username = findViewById(R.id.main_nameEdit);
        password = findViewById(R.id.main_PasswordEdit);
        login = findViewById(R.id.mian_login);
        registrationBtn = findViewById(R.id.mian_registration);
    }

}
